<?php
class User_model extends CI_Model {
    
    function __construct()
    {
        parent::__construct();
    }
    function them_user($capnguoidung,$taikhoan,$matkhau,$ten)
    {
        $data=array
        (
        'ten_nguoidung'=>$taikhoan,
        'matkhau_nguoidung'=>md5($matkhau),
        'tenthat_nguoidung'=>$ten,
        'capnguoidung'=>$capnguoidung,
        'trangthai_nguoidung'=>1
        );
        $this->db->insert('nguoidung',$data);
        return $this->db->insert_id(); 
    }
    function sua_user($id,$capnguoidung,$taikhoan,$matkhau,$ten)
    {
        $data=array
        (
        'ten_nguoidung'=>$taikhoan,
        'matkhau_nguoidung'=>md5($matkhau),
        'tenthat_nguoidung'=>$ten,
        'capnguoidung'=>$capnguoidung,
        );
        $this->db->where('id_nguoidung',$id);
        $result=$this->db->update('nguoidung',$data);
        return $result;
    }
    function xoa_user($id)
    {
        $result=$this->db->delete('nguoidung',array('id_nguoidung'=>$id));
        return $result;
    }
    function thongtinUser($id)
    {
        $this->db->where('id_nguoidung',$id);
        $query=$this->db->get('nguoidung');
        return $query->result_array();
    }
    function getUsername($id)
    {
        $this->db->select('ten_nguoidung');
        $this->db->where('id_nguoidung',$id);
        $query=$this->db->get('nguoidung');
        foreach($query->result_array() as $row);
        return $row['ten_nguoidung'];
    }
    function themQuyen($userID,$dsQuyen)
    {
        foreach($dsQuyen as $row)
        {
            $data=array(
            'id_nguoidung'=>$userID,
            'id_quyen'=>$row
            );
        $result=$this->db->insert('nguoidung_quyen',$data);
        }
        return $result;
    }
    function getIdQuyen($tenQuyen)
    {
        $this->db->select('id_quyen');
        $this->db->where('ten_quyen',$tenQuyen);
        $query=$this->db->get('quyen');
        if($query)
            {
                return $query->row()->id_quyen;
            }
            return false;
    }
    function dsQuyen($nhomquyen)
    {
        $this->db->where('nhom_quyen',$nhomquyen);
        $query=$this->db->get('quyen');
        return $query->result();
    }
    function deleteQuyen($userID)
        {
            $this->db->where('id_nguoidung',$userID);
            $query=$this->db->delete('nguoidung_quyen');
            return $query;
        }
    function getListQuyen($userID)
    {
        $this->db->where('id_nguoidung',$userID);
        $this->db->select('id_quyen');
        $query=$this->db->get('nguoidung_quyen');
        return $query->result_array();
    }
}
?>